package com.baidu.process.chat.parser;

import com.alibaba.fastjson.JSONObject;
import com.baidu.process.chat.core.web.bean.IMRequestServer;
import com.baidu.process.chat.core.web.bean.IMResponseResult;
import com.baidu.process.chat.parser.bean.QueryJsonStr;
import com.baidu.process.chat.parser.sourceHandler.BaseSourceHandler;
import com.baidu.process.chat.parser.sourceHandler.SourceHandlerFactory;
import com.baidu.process.ngd.bean.NGDResBody;
import com.baidu.process.ngd.bean.NGDStartResBody;
import com.baidu.process.ngd.service.impl.NGDServiceImpl;
import com.baidu.process.record.bean.DialogueRecord;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:fangyuchao
 * @Description:
 * @Date: Create in 9:44 2022/11/7
 */
@Service
public class QueryParseNgdResult implements ResultPaser<NGDResBody> {
    private static final Logger logger= LoggerFactory.getLogger(QueryParseNgdResult.class);

    @Value("#{'${ngd.filter.answerActions}'.split(',')}")
    private List<String> answerActions;
    @Autowired
    SourceHandlerFactory sourceHandlerFactory;
    @Override
    public IMResponseResult parseResult(NGDResBody ngdResBody, IMRequestServer imRequestServer, DialogueRecord dialogueRecord) {
        NGDResBody.NGDQueryData queryData=null;
        IMResponseResult imResponseResult = new IMResponseResult();
        try{
            queryData = ngdResBody.getData();
            //通过ngd返回的source来处理不同的返回报文
            BaseSourceHandler sourceHandler =  sourceHandlerFactory.chooseSourceHandler(queryData.getSource());
            QueryJsonStr queryJsonStr = sourceHandler.handleQuery(ngdResBody,imRequestServer, dialogueRecord);
            imResponseResult.setJsonString(JSONObject.toJSONString(queryJsonStr));
            imResponseResult.setReturnCode("200");
            imResponseResult.setReturnMessage("");
            return imResponseResult;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("[sessionid:{}]ngd返回报文解析处理异常,NGD报文:[{}]",queryData.getSessionId(),ngdResBody);
            imResponseResult.setReturnCode("1010");
            imResponseResult.setReturnMessage("ngd返回报文解析处理异常");
            return imResponseResult;
        }
    }



}
